package com.omg.server.framework.ibatis;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ibatis.sqlmap.engine.execution.SqlExecutor;
import com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback;
import com.ibatis.sqlmap.engine.scope.RequestScope;

public class IBatisSqlExecutor extends SqlExecutor {
	private static final Log logger = LogFactory.getLog(IBatisSqlExecutor.class);
	private Dialect dialect;  
	  
    private boolean enablePage = true;  
  
    public Dialect getDialect() {  
        return dialect;  
    }  
  
    public void setDialect(Dialect dialect) {  
        this.dialect = dialect;  
    }  
  
    public boolean isEnablePage() {
		return enablePage;
	}

	public void setEnablePage(boolean enablePage) {
		this.enablePage = enablePage;
	}

	/** 
     * 重写SqlExecutor.executeQuery方法 实现ORACLE的SQL物理分页 see OracleDialect 
     */  
    public void executeQuery(RequestScope request, Connection conn,  String sql, Object[] parameters,
    		                int skipResults, int maxResults, RowHandlerCallback callback) throws SQLException {  
        if ((skipResults != NO_SKIPPED_RESULTS || maxResults != NO_MAXIMUM_RESULTS) && enablePage) {// 有分页信息、可物理分页SQL 
            
        	sql = dialect.getPageSql(sql, skipResults, maxResults);// 获得物理分页SQL
            if (logger.isDebugEnabled()) {
                logger.debug("分页SQL:"+sql);
            }
            skipResults = NO_SKIPPED_RESULTS;// 设置skipResults为SqlExecutor不分页 
            maxResults = NO_MAXIMUM_RESULTS;// 设置maxResults为SqlExecutor不分页 
        }
        
        //调用SqlExecutor查询方法   
        super.executeQuery(request, conn, sql, parameters, skipResults, maxResults, callback);
    }  
    
}
